require('Sound')
require('World')
require('Menu')

menus = {}
menus[1] = {
     title="The Higgs Boson Supernova Experience",
     {t="New Game", cb="newGame"},
     {t="Credits", cb="cred"},
     {t="Exit", cb="exit"}
}
menus[2] = {
     title="Credits",
     desc="Joey Green - Collectibles, Parasites, Glow\nAlan Layfield - Player, Expanse, Black Holes\nJoseph Myers - Audio, Menus\nDr. T.J. Jankun-Kelly - Game Name\n\n\nSpecial Thanks for Music:\n\nZero8\nby Copter\n\nSunrise on Mars\nby Jason Shaw",
     {t="Back", cb="back"}
}

TESTING = false
gameOver = false
brightness = 0
BHTest = false
testOutput = -1
startTime = 0
curTime = 0
timeElapsed = 0
minsElapsed = 0

playerControl = false
--tutClock initialized at opening() in EventHandler
font_tut = love.graphics.newFont("Fonts/Orbitron Black.ttf", 14)

function love.load()
	small = love.graphics.newFont(12)
	world = World.load(10000,10000)
     Sound.load()
	menu:toggle()
     menu:load(menus)
end

function love.update(dt)
	if menu.run then
          menu:update(dt)
     elseif gameOver then
          brightness = brightness + dt*25
          if brightness > 255 then
               brightness = 255
          end
     else
          if playerControl == false then
               world.eventHandler:opening()
          end
               
          world:update(dt)
     end
     Sound:update(dt, world.player.health, world.glow.health)
end

function love.draw()
	
	if menu.run then
          menu:draw()
     
     elseif gameOver then
     
          love.graphics.setColor(255, 255, 255)  
          love.graphics.rectangle('fill', 0, 0, love.graphics.getWidth(), love.graphics.getHeight())
          
          love.graphics.setBlendMode('alpha')
          love.graphics.setColor(0, 0, 0, brightness)
          love.graphics.print('The End', love.graphics.getWidth()/2 - font_end:getWidth('The End')/2, love.graphics.getHeight()*0.2)

		  
		  if timeElapsed >= 60 then
			minsElapsed = math.floor(timeElapsed/60)
		  end
		  minOutput = minsElapsed
		  if minsElapsed < 10 then
			if minsElapsed == 0 then
				minOutput = "00"
			elseif minsElapsed == 1 then
				minOutput = "01"
			elseif minsElapsed == 2 then
				minOutput = "02"
			elseif minsElapsed == 3 then
				minOutput = "03"
			elseif minsElapsed == 4 then
				minOutput = "04"
			elseif minsElapsed == 5 then
				minOutput = "05"
			elseif minsElapsed == 6 then
				minOutput = "06"
			elseif minsElapsed == 7 then
				minOutput = "07"
			elseif minsElapsed == 8 then
				minOutput = "08"
			else
				minOutput = "09"
			end
		end
		
		secsElapsed = timeElapsed % 60
			secOutput = secsElapsed
		if secsElapsed < 10 then
			if secsElapsed == 0 then
				secOutput = "00"
			elseif secsElapsed == 1 then
				secOutput = "01"
			elseif secsElapsed == 2 then
				secOutput = "02"
			elseif secsElapsed == 3 then
				secOutput = "03"
			elseif secsElapsed == 4 then
				secOutput = "04"
			elseif secsElapsed == 5 then
				secOutput = "05"
			elseif secsElapsed == 6 then
				secOutput = "06"
			elseif secsElapsed == 7 then
				secOutput = "07"
			elseif secsElapsed == 8 then
				secOutput = "08"
			else
				secOutput = "09"
			end
		end	
		  
		love.graphics.printf('Survival Time', love.graphics.getWidth()/2 - font_end:getWidth('Survival Time')/2, love.graphics.getHeight() * .5, 500, "left")
        love.graphics.printf(minOutput, love.graphics.getWidth()/2 + font_end:getWidth(':')/2 - 91, love.graphics.getHeight() * .5 + 50, 500, "left")
		love.graphics.printf(':', love.graphics.getWidth()/2 - font_end:getWidth(':')/2, love.graphics.getHeight()*.5 + 50 - 2, 500, "left")
		love.graphics.printf(secOutput, love.graphics.getWidth()/2 - font_end:getWidth(':')/2 + 15, love.graphics.getHeight()*.5 + 50, 500, "left")
     else
	
		if TESTING == true then
			love.graphics.setFont(small)
		
          --Diagnostic Info
          love.graphics.print('Current Position {', 5, 35)
          love.graphics.print(world.eventHandler.currentPos[1], 140, 35)
          love.graphics.print(world.eventHandler.currentPos[2], 180, 35)
          love.graphics.print(' }', 220, 35)

          love.graphics.print('Player Health: ', 5, 5)
          love.graphics.print(world.player.health, 100, 5)
          
          love.graphics.print('Glow Health: ', 5, 20)
          love.graphics.print(world.glow.health, 100, 20)
          
          love.graphics.setColor(255,255,255,255)
          love.graphics.line(0, 0, 0, 5000)
          love.graphics.line(0, 0, 5000, 0)
          love.graphics.line(0, 5000, 5000, 5000)
          love.graphics.line(5000, 0, 5000, 5000)

          fps = love.timer.getFPS()
          love.graphics.print('FPS: ', 5, 50)
          love.graphics.print(fps, 50, 50)
		  
		  love.graphics.print('Pulse: ', 5, 85)
		  if world.player.isPulseActive == false then
			love.graphics.print('false', 65, 85)
		  else
			love.graphics.print('true', 65, 85)
		  end
		  
		  love.graphics.print('Z1: ', 5, 100)
		  love.graphics.print(world.itemsInZones[1], 50, 100)
		  love.graphics.print('Z2: ', 5, 115)
		  love.graphics.print(world.itemsInZones[2], 50, 115)
		  love.graphics.print('Z3: ', 5, 130)
		  love.graphics.print(world.itemsInZones[3], 50, 130)
		  
		  love.graphics.print('# Black Holes: ', 5, 145)
		  love.graphics.print(#world.blackHoleList, 100, 145)
          
		  if BHTest == false then
			love.graphics.print('false', 5, 160)
		  else
			love.graphics.print('true', 5, 160)
		  end
		  
		  love.graphics.print('Distance: ', 5, 175)
		  love.graphics.print(testOutput, 100, 175)
		end

			tmpTime = (os.time())
			if tmpTime > curTime then
				timeElapsed = timeElapsed + 1
			end
		  curTime = tmpTime
		  if timeElapsed > 60 then
			minsElapsed = math.floor(timeElapsed/60)
		  end
		  minOutput = minsElapsed
		  if minsElapsed < 10 then
			if minsElapsed == 0 then
				minOutput = "00"
			elseif minsElapsed == 1 then
				minOutput = "01"
			elseif minsElapsed == 2 then
				minOutput = "02"
			elseif minsElapsed == 3 then
				minOutput = "03"
			elseif minsElapsed == 4 then
				minOutput = "04"
			elseif minsElapsed == 5 then
				minOutput = "05"
			elseif minsElapsed == 6 then
				minOutput = "06"
			elseif minsElapsed == 7 then
				minOutput = "07"
			elseif minsElapsed == 8 then
				minOutput = "08"
			else
				minOutput = "09"
			end
		end
		
		secsElapsed = timeElapsed % 60
			secOutput = secsElapsed
		if secsElapsed < 10 then
			if secsElapsed == 0 then
				secOutput = "00"
			elseif secsElapsed == 1 then
				secOutput = "01"
			elseif secsElapsed == 2 then
				secOutput = "02"
			elseif secsElapsed == 3 then
				secOutput = "03"
			elseif secsElapsed == 4 then
				secOutput = "04"
			elseif secsElapsed == 5 then
				secOutput = "05"
			elseif secsElapsed == 6 then
				secOutput = "06"
			elseif secsElapsed == 7 then
				secOutput = "07"
			elseif secsElapsed == 8 then
				secOutput = "08"
			else
				secOutput = "09"
			end
		end
          
		  love.graphics.setFont(small)
			
          world:draw()
		  
          love.graphics.rectangle('fill', 0,0,love.graphics.getWidth(), love.graphics.getHeight())
          
     end
		
end

